<?php

namespace app\admin\controller\eduadmin;
use think\Session;
use app\admin\model\Admin;
use app\admin\model\CalendarEvent;
use app\common\controller\Backend;
use think\exception\PDOException;

/**
 * 课程管理
 *
 * @icon fa fa-circle-o
 */
class Classschedule extends Backend
{
    
    /**
     * Classschedule模型对象
     */
    protected $model = null;
    protected $childrenAdminIds = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Classschedule');
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("weekList", $this->model->getWeekList());
        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
       /**
     * 查看
     */
    public function index()
    {
        //读取管理员列表
        $adminList = Admin::where('id', 'in', $this->childrenAdminIds)->field('id,nickname as name')->select();
        $this->assignconfig('admins', $adminList);

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }

            $start = $this->request->get('start');
            $end = $this->request->get('end');
            $type = $this->request->get('type');
            $admin_id = $this->request->get('admin_id');
            if (!$admin_id)
            {
                $admin_id = $type == 'my' ? $this->auth->id : 0;
            }
            $adminIds = $admin_id ? [$admin_id] : $this->childrenAdminIds;

            $list = model('Schedule')
                    ->where('teacherid', 'in', $adminIds)
                    ->where('starttime', 'between', [strtotime($start), strtotime($end)])
                    ->order('id desc')
                    ->select();
            $result = [];
            $time = time();
            foreach ($list as $k => $v)
            {
                $result[] = $v['render'];
            }
            return json($result);
        }

        $classList = model('Schedule')->where('starttime',0)->order('updatetime desc')->select();
        $this->view->assign("classList", $classList);
        return $this->view->fetch();
    }

     /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
              
                try
                {
                   //获取登录用户
                    $admin =Session::get("admin");                   
                    //创建人
                    $params["createby"] = $admin["username"];

                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    if(input('type')=='event'){
                        $params['starttime'] = 0;
                        $params['endtime'] = 0;
                    }
                    $params['status'] = 0;
                    $result = $this->model->allowField(true)->create($params);
                    if ($result !== false)
                    {
                        $data = $result->render;
                        $this->success('', null, $data);
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

     /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds))
        {
            if (!in_array($row[$this->dataLimitField], $adminIds))
            {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    //获取登录用户
                    $admin =Session::get("admin");                   
                    //创建人
                    $params["updateby"] = $admin["username"];

                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $data = $row->render;
                        $this->success('', null, $data);
                    }
                    else
                    {
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

 
         /**
     * Selectpage搜索
     * 
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }
}
